package com.zzedu.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzedu.common.cache.CacheNameContansts;
import com.zzedu.common.core.domain.entity.SysDictData;
import com.zzedu.common.core.service.ISysDictDataService;
import com.zzedu.common.utils.StringUtils;
import com.zzedu.system.domain.SysConfig;
import com.zzedu.system.mapper.SysDictDataMapper;
import org.apache.commons.collections4.MapUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 字典 业务层处理
 *
 * @author smarthome
 */
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements ISysDictDataService {

    /**
     * 缓存名称
     */
    final String cacheName = CacheNameContansts.SYS_DICT;

    /**
     * 根据条件分页查询字典数据
     *
     * @param dictData 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public IPage<SysDictData> selectDictDataList(IPage page, SysDictData dictData) {
        LambdaQueryWrapper<SysDictData> queryWrapper = Wrappers.lambdaQuery(SysDictData.class);
        queryWrapper.eq(StringUtils.isNotBlank(dictData.getDictType()), SysDictData::getDictType, dictData.getDictType());
        queryWrapper.eq(StringUtils.isNotBlank(dictData.getStatus()), SysDictData::getStatus, dictData.getStatus());
        queryWrapper.like(StringUtils.isNotBlank(dictData.getDictLabel()), SysDictData::getDictLabel, dictData.getDictLabel());
        Map map = dictData.getParams();
        if (!MapUtils.isEmpty(map)) {
            if (map.containsKey("beginTime")) {
                queryWrapper.ge(SysDictData::getGmtCreate, MapUtils.getString(map, "beginTime"));
            }
            if (map.containsKey("endTime")) {
                queryWrapper.le(SysDictData::getGmtCreate, MapUtils.getString(map, "endTime"));
            }
        }
        queryWrapper.orderByDesc(SysDictData::getId);
        return this.page(page, queryWrapper);

    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictType  字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    @Cacheable(value = cacheName, key = "#p0+'_'+#p1")
    public String selectDictLabel(String dictType, String dictValue) {
        LambdaQueryWrapper<SysDictData> queryWrapper = Wrappers.lambdaQuery(SysDictData.class);
        queryWrapper.eq(SysDictData::getDictType, dictType);
        queryWrapper.eq(SysDictData::getDictValue, dictValue);
        queryWrapper.last("limit 1");
        SysDictData sysDictData = getOne(queryWrapper);
        return null == sysDictData ? null : sysDictData.getDictLabel();
    }

    /**
     * 批量删除字典数据
     *
     * @param ids 需要删除的数据
     * @return 结果
     */
    @Override
    @CacheEvict(cacheNames = cacheName, allEntries = true)
    public void deleteDictDataByIds(Long[] ids) {
        removeBatchByIds(Arrays.stream(ids).collect(Collectors.toList()));
    }

    /**
     * 新增保存字典数据信息
     *
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    @CacheEvict(cacheNames = cacheName, allEntries = true)
    public int insertDictData(SysDictData data) {
        int row = baseMapper.insert(data);
        return row;
    }

    /**
     * 修改保存字典数据信息
     *
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    @CacheEvict(cacheNames = cacheName, allEntries = true)
    public int updateDictData(SysDictData data) {
        int row = baseMapper.updateById(data);
        return row;
    }
}
